<?php
echo '<?xml version="1.0" encoding="UTF-8" />';
echo $this->doctype();
$config = Zend_Registry::get('config');?>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<?php 
	$this->headLink()->prependStylesheet($config->baseurl.'/css/layout.css');
	$this->headLink()->prependStylesheet($config->baseurl.'/css/reset.css');
	
	echo $this->headLink(); ?>
</head>

<div style="text-align:left;">

<div class="successResults">To view information from that area.</div>
<?php echo $this->form;?>
		
</div>
<script type="text/javascript">
$(document).ready(function()
{	
    if ($("select#city option").length > 1){
    	$("#city-label,#city-element").show();
    }
    if ($("select#region option").length > 1){
    	$("#region-label,#region-element").show();
    }

    $baseUrl=$("#baseUrl").text();  
    
    //live change doesn't work in ie jquery 1.4.2
    $("#settingForm #country, #registerForm #country, #locationLoggedIn #country, #locationUnLoggedIn #country").bind("change",function(){
    	$formId = $(this).parents("form").attr("id");
    	
    	$.ajax({
    		  url: $baseUrl+ '/user/citylistajax',
    		  type: 'post',
    		  dataType: 'json',
    		  async: false,
    		  data: ({countryId : parseInt($(this).val())}),
    		  success: function(data) {
    			if(data.exist) {
    				$("form[id='"+$formId+"']").find("#city").empty().append(data.list);
    				$("form[id='"+$formId+"']").find("#city-label,#city-element").show();
    			} else {
    				$("form[id='"+$formId+"']").find("#city").empty().append(data.list);
    				$("form[id='"+$formId+"']").find("#city-label,#city-element").hide();
    				$("form[id='"+$formId+"']").find("#region-label,#region-element").hide();
    			}
    		  }});   
    });
    
    $("#settingForm #city, #registerForm #city, #locationLoggedIn #city, #locationUnLoggedIn #city").bind("change",function(){
    	
    	$formId = $(this).parents("form").attr("id");
    	
    	$.ajax({
    		  url: $baseUrl+ '/user/regionlistajax',
    		  type: 'post',
    		  dataType: 'json',
    		  async: false,
    		  data: ({cityId : parseInt($(this).val())}),
    		  success: function(data) {
    			if(data.exist) {
    				$("#"+$formId + " #region").empty().append(data.list);
    				$("#"+$formId + " #region-label, #"+$formId + " #region-element").show();
    			//	$("form[id='"+$formId+"']").find("#region").empty().append(data.list);
    			//	$("form[id='"+$formId+"']").find("#region-label,#region-element").show();
    			} else {
    				$("#"+$formId + " #region").empty().append(data.list);
    				$("#"+$formId + " #region-label, #"+$formId + " #region-element").hide();
    				//$("form[id='"+$formId+"']").find("#region").empty().append(data.list);
    				//$("form[id='"+$formId+"']").find("#region-label,#region-element").hide();
    			}
    		  }});   
    });
    
    
    $("#locationLoggedIn, #locationNonLoggedIn").submit(function(e){
    	e.preventDefault();
    	   	
    	if ($("#locationLoggedIn #region-element").length > 0) {
    		//LoggedIn
    		
    		if ($("#locationLoggedIn #region-element").css("display")=="block") {
    			//region is provided
    			$cityId = $('#locationLoggedIn select#region option:selected').val();

    	    	if ($cityId == 0){
    	    		$("#locationLoggedIn #region-element")
    	    		.append('<label for="region" generated="true" class="error">Please select one region ...</label>');
    	    	}
    	    	
    		} else { 
    			//region is not provided
    			$cityId = $('#locationLoggedIn select#city option:selected').val();
    			if ($cityId == 0){
    	    		$("#locationLoggedIn #city-element")
    	    		.append('<label for="city" generated="true" class="error">Please select one city ...</label>');
    	    	} 
    		}
    	} else {
    		//Not LoggedIn, or loggedIn but No region is provided  
		
    		$cityId = $('#locationNonLoggedIn select#city option:selected').val();
	    	if ($cityId == 0){
	    		$("#locationNonLoggedIn #city-element")
	    		.append('<label for="city" generated="true" class="error">Please select one city ...</label>');
	    	} 
    	}
    	
    	if ($cityId != 0){
    		$.ajax({
    			url: $baseUrl+ '/user/locationajax',
    			type: 'post',
    			async: false,
    			data: ({cityId : parseInt($cityId)}),
    			success: function(data) {
    			location.href=location.href;
  		  	}}); 
    	}
    	
    });
    
	
});
</script>